//
//  CurrencyDetailsViewModel.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/5/8.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit
enum orderDetailType {
    case orderCoinHistoryType
    case leverPositionType
    case leverHistory
}
class CurrencyDetailsViewModel: BaseViewModel {
    var model:CoinOrderModel = CoinOrderModel() {
        didSet {
            if type == .orderCoinHistoryType {
                self.changeTitleArr()
            }else if type == .leverPositionType {
                self.changeLeverPositon()
            }else {
                self.changeLverHistory()
            }
            statesStr = String(format: "%@%@",model.tradeType == 0 ? "市价" : "限价"  ,model.position == 0 ? "买入" : "卖出")
        }
    }
    var isCurrent:Bool = false //标记当前委托
    var titleArr = [[String]]()
    var dataArr = [[String]]()
    var type: orderDetailType! {
        didSet {
        }
    }
    
    var statesStr:String = "" //状态
    
    func changeTitleArr()  {
        var arr:[String] = []
        var secondArr:[String] = []
        arr.append(String(format: "%@(%@)", kText("委托价"),model.sourceCoin))
        var tradeTitle:String = kText("委托量")
        var tradeCoinName:String = ""
        if (model.tradeType == 0 && model.position == 0) {
            tradeTitle = kText("委托额")
            tradeCoinName = model.sourceCoin
        }else {
            tradeCoinName = model.targetCoin
        }
        var poundageName:String = ""
        if model.position == 0 {
            poundageName = model.targetCoin
        }else {
            poundageName = model.sourceCoin
        }
        
        arr.append(String(format: "%@(%@)", tradeTitle,tradeCoinName))
        arr.append(String(format: "%@(%@)", kText("委托总额"),(model.tradeType == 0 && model.position != 0) ?  model.targetCoin : model.sourceCoin))
        arr.append(String(format: "%@(%@)", kText("手续费"),poundageName))
        
        secondArr.append(String(format: "%@(%@)", kText("成交均价"),model.sourceCoin))
        secondArr.append(String(format: "%@(%@)", kText("已成交"),model.targetCoin))
        secondArr.append(String(format: "%@(%@)", kText("成交总额"),(model.tradeType == 0 && model.position != 0) ?  model.targetCoin : model.sourceCoin))
        secondArr.append(String(format: "%@",kText("时间")))
        titleArr.append(arr)
        titleArr.append(secondArr)
        
        self.model.archiveAllPrice()
        let infoArr:[String] = [model.tradeType == 0 ? kText("市价") : CYCalculate.cy_intercept(number: self.model.tradePrice, digit: 6)!,CYCalculate.cy_intercept(number: self.model.tradeAmount, digit: 6)!,model.totalPrice.count != 0 ? model.totalPrice: "0",CYCalculate.cy_intercept(number: model.poundageAmount, digit: 6)!]
        
        let timeStr = model.realTime
        
    
        let secondInfoArr:[String] = [CYCalculate.cy_intercept(number: self.model.dealPrice, digit: 6)!,self.model.haveDeal,model.allPrice.count != 0 ? model.allPrice : "0",timeStr]
        
        dataArr.append(infoArr)
        dataArr.append(secondInfoArr)
    }
    
    func changeLeverPositon()  {
        var arr:[String] = []
        var secondArr:[String] = []
        var thirdArr:[String] = []
        arr.append(kText("浮动盈亏"))
        arr.append(kText("交易品种"))
        
        secondArr.append(String(format: "%@(%@)", kText("开仓数量"),model.targetCoin))
        secondArr.append(String(format: "%@(%@)", kText("开仓均价"),model.sourceCoin))
        secondArr.append(String(format: "%@(%@)", kText("当前价格"),model.sourceCoin))
        secondArr.append(kText("止损总额"))
        secondArr.append(kText("止赢总额"))
        
        thirdArr.append(kText("订单号"))
        thirdArr.append(kText("开仓时间"))
        
        titleArr.append(arr)
        titleArr.append(secondArr)
        titleArr.append(thirdArr)
        var timeStr = ""
        if !isCurrent && model.dealTime != ""{
            let dateStr:Int = Helper.timeWithIntervar(timeStr: model.dealTime)
            timeStr = Helper.timeWithTimeIntervalString(timeStr: dateStr)
        }
        
         let infoArr:[String] = []
        
        let secondInfoArr:[String] = [CYCalculate.cy_intercept(number: self.model.holdPosition, digit: 6)!,"","",CYCalculate.cy_intercept(number: model.stopLoss, digit: 6)!,CYCalculate.cy_intercept(number: model.stopProfit, digit: 6)!]
        let thirdInfoArr:[String] = [model.orderNo,timeStr]
        dataArr.append(infoArr)
        dataArr.append(secondInfoArr)
        dataArr.append(thirdInfoArr)
    }
    
    func changeLverHistory()  {
        var arr:[String] = []
        var secondArr:[String] = []
        arr.append(kText("浮动盈亏"))
        arr.append(kText("交易品种"))
        arr.append(kText("平仓方式"))
        
//        entrustNameLabel?.text = String(format: "%@(%@)",kText("委托量"),(model.tradeType == 0 && model.position == 0) ? model.sourceCoin : model.targetCoin)
//
//        amountNameLabel?.text = String(format: "%@(%@)",kText("委托总额"),(model.tradeType == 0 && model.position != 0) ?  model.targetCoin : model.sourceCoin)
//        dealNameLabel?.text = String(format: "%@(%@)",kText("已成交"),(model.tradeType == 0 && model.position == 0) ? model.sourceCoin : model.targetCoin)
        
        secondArr.append(String(format: "%@(%@)", kText("开仓均价"),model.sourceCoin))
        secondArr.append(String(format: "%@(%@)", kText("成交量"),model.sourceCoin))
        secondArr.append(String(format: "%@(%@)", kText("成交总额"),model.sourceCoin))
        secondArr.append(String(format: "%@(%@)", kText("平仓均价"),model.sourceCoin))
        secondArr.append(String(format: "%@(%@)", kText("平仓量"),model.sourceCoin))
        secondArr.append(String(format: "%@(%@)", kText("成交总额"),model.sourceCoin))
        secondArr.append(kText("止损总额"))
        secondArr.append(kText("止赢总额"))
        
        secondArr.append(String(format: "%@(%@)", kText("委托价"),model.sourceCoin))
        secondArr.append(String(format: "%@(%@)", kText("委托量"),(model.tradeType == 0 && model.position == 0) ? model.sourceCoin : model.targetCoin))
        
        titleArr.append(arr)
        titleArr.append(secondArr)
    }

}
